var HpFont         = LoadFont("Hp.rfn");
var MpFont         = LoadFont("Mp.rfn");

//The main part of the battle system...
function BattleMenuPlayer1()
{
UpdatePlayers();
eatKey();
  if (Battle.Talk == true)
  {
    if (Monster1.name == "Ricator")
    {
    BattleTalk("What?  A Ricator?  It must feeding on the food supply of the local animals. forcing them to attack passers by.  Be careful, it can heal itself!", Player.port);  
    }
  Battle.Talk = false;
  }

  if (Player.moddef == true)
  {
  Player.moddef = false;
  Player.defence = Math.floor(Player.defence/2);
  }

  if (Battle.monsternum == 0)
  {
  BattleEnd();
  }
  else
  {
  RenderMap();
  Player.active = "Player1";
    if (Player.hp > 0)
    {
      if (Player.stun == 0)
      {
      BattleName(); 
      DrawPlayerStats();
    
      var BattleMenu = new Menu();
      BattleMenu.font         = Font;
      BattleMenu.window_style = WindowStyle;
      BattleMenu.arrow        = Arrow;
      BattleMenu.addItem("Fight",  BattleMenu_Fight);
        if (Player.chrclass == "Soldier")
        {
        BattleMenu.addItem("Sword Tech", BattleSpecialSoldierTech);
        }
        else
        {
        BattleMenu.addItem("Special",  BattleMenu_SpecialP1);
        }
      BattleMenu.addItem("Item",  BattleMenu_Item);
      BattleMenu.addItem("Defend",  BattleMenu_Defend);
      BattleMenu.addItem("Run", BattleMenu_Run);
      BattleMenu.execute(5, 355, 235, 120);
      }
      else
      {
      Player.stun = Player.stun - 1;
      BattleTalk(Player.name + " is still stunned.", Player.port);
      BattleMenuPlayer2();
      }
    }
    else
    {
    BattleMenuPlayer2();
    }
  }
}
function BattleMenuPlayer2()
{
UpdatePlayers();
eatKey();

  if (Player2.moddef == true)
  {
  Player2.moddef = false;
  Player2.defence = Math.floor(Player2.defence/2);
  }

  if (Battle.monsternum == 0)
  {
  BattleEnd();
  }
  else
  {
    RenderMap();
    if (Player2.pres == true)
    {
      Player.active = "Player2";
      if (Player2.hp > 0)
      { 
        if (Player2.stun == 0)
        {      
        BattleName(); 
        DrawPlayerStats();
        
        var BattleMenu = new Menu();
        BattleMenu.font         = Font;
        BattleMenu.window_style = WindowStyle;
        BattleMenu.arrow        = Arrow;
        BattleMenu.addItem("Fight",  BattleMenu_Fight);
        
          if (Player2.chrclass == "Soldier")
          {
          BattleMenu.addItem("Sword Tech", BattleSpecialSoldierTech);
          }
          else
          {
          BattleMenu.addItem("Special",  BattleMenu_SpecialP1);
          }
        BattleMenu.addItem("Item",  BattleMenu_Item);
        BattleMenu.addItem("Defend",  BattleMenu_Defend);
        BattleMenu.addItem("Run", BattleMenu_Run);
        BattleMenu.execute(5, 355, 235, 120);
        }
        else
        {
        Player2.stun = Player2.stun - 1;
        BattleTalk(Player2.name + " is still stunned.", Player2.port);
        BattleMenuPlayer3();
        }
      }
      else
      {
      BattleMenuPlayer3();
      }
    }
    else
    {
    BattleMenuPlayer3();
    }
  }
}
function BattleMenuPlayer3()
{
UpdatePlayers();
eatKey();
  if (Player3.moddef == true)
  {
  Player3.moddef = false;
  Player3.defence = Math.floor(Player3.defence/2);
  }

  if (Battle.monsternum == 0)
  {
  BattleEnd();
  }
  else
  {
    RenderMap();
    if (Player3.pres == true)
    {
      Player.active = "Player3";
      if (Player3.hp > 0)
      {
        if (Player3.stun == 0)
        {
        
        BattleName();
        DrawPlayerStats();
        
        var BattleMenu = new Menu();
        BattleMenu.font         = Font;
        BattleMenu.window_style = WindowStyle;
        BattleMenu.arrow        = Arrow;
        BattleMenu.addItem("Fight",  BattleMenu_Fight);
        
          if (Player3.chrclass == "Soldier")
          {
          BattleMenu.addItem("Sword Tech", BattleSpecialSoldierTech);
          }
          else
          {
          BattleMenu.addItem("Special",  BattleMenu_SpecialP1);
          }
        BattleMenu.addItem("Item",  BattleMenu_Item);
        BattleMenu.addItem("Defend",  BattleMenu_Defend);
        BattleMenu.addItem("Run", BattleMenu_Run);
        BattleMenu.execute(5, 355, 235, 120);
        }
        else
        {
        Player3.stun = Player3.stun - 1;
        BattleTalk(Player3.name + " is still stunned.", Player3.port);
        MonstersTurn();
        }
      }
      else
      {
      MonstersTurn();
      }
    }
    else
    {
    MonstersTurn();
    }
  }
}
function BattleMenu_Defend()
{
  if (Player.active == "Player1")
  {
  Player.moddef = true;
  Player.defence = Player.defence*2;
  BattleMenu_Itemused();
  }
  else if (Player.active == "Player2")
  {
  Player2.moddef = true;
  Player2.defence = Player2.defence*2;
  BattleMenu_Itemused();
  }
  else if (Player.active == "Player3")
  {
  Player3.moddef = true;
  Player3.defence = Player3.defence*2;
  BattleMenu_Itemused();
  }
}
function BattleMenu_Fight()
{
  Player.BMenuTemp = true;

  LoadFightStats();
  RenderMap();
  
    BattleName();
    DrawPlayerStats();

    var BattleMenu = new BMenu();
    BattleMenu.font         = Font;
    BattleMenu.window_style = WindowStyle;
    BattleMenu.arrow        = Arrow;

    if (Monster1.hp >= 1)
    {
    BattleMenu.addItem(Monster1.name,  BattleMenu_FightMonster1);
    }
    if (Monster2.hp >= 1)
    {
    BattleMenu.addItem(Monster2.name, BattleMenu_FightMonster2);
    }
    if (Monster3.hp >= 1)
    {
    BattleMenu.addItem(Monster3.name, BattleMenu_FightMonster3);
    }
    if (Monster4.hp >= 1)
    {
    BattleMenu.addItem(Monster4.name, BattleMenu_FightMonster4);
    }
    BattleMenu.addItem("Cancel", GoToActivePlayer);
    BattleMenu.execute(5, 355, 235, 120);

}

function BattleMenu_FightMonster1()
{
ShowPersonFight();
InjureFrame("Monster1");

    Player.attackmiss = "";
    Battle.math = Math.random()/5 + 0.5;
    RenderMap();

    if (Math.random() <= Monster1.evade/100)
    {
    Player.attackmiss = "Miss";
    }
    else
    {
    Player.attackdamage = Math.floor(Player.tempstr*Player.tempattack*Battle.math/Monster1.defence);
    Monster1.hp = Monster1.hp - Player.attackdamage;
    }
    ShowMonster1Damage(Player.attackdamage);
    BattleMenu_Itemused();
}  

function BattleMenu_FightMonster2()
{
ShowPersonFight();
InjureFrame("Monster2");

    Player.attackmiss = "";
    Battle.math = Math.random()/5 + 0.5;
    RenderMap();
    if (Math.random() <= Monster2.evade/100)
    {
    Player.attackmiss = "Miss";
    }
    else
    {
    Player.attackdamage = Math.floor(Player.tempstr*Player.tempattack*Battle.math/Monster2.defence);
    Monster2.hp = Monster2.hp - Player.attackdamage;
    }
    ShowMonster2Damage(Player.attackdamage)
    BattleMenu_Itemused();
}
function BattleMenu_FightMonster3()
{
ShowPersonFight();
InjureFrame("Monster3");

    Player.attackmiss = "";
    Battle.math = Math.random()/5 + 0.5;
    RenderMap();
    if (Math.random() <= Monster3.evade/100)
    {
    Player.attackmiss = "Miss";
    }
    else
    {
    Player.attackdamage = Math.floor(Player.tempstr*Player.tempattack*Battle.math/Monster3.defence);
    Monster3.hp = Monster3.hp - Player.attackdamage;
    }
    ShowMonster3Damage(Player.attackdamage)
    BattleMenu_Itemused();
}
function BattleMenu_FightMonster4()
{
ShowPersonFight();
InjureFrame("Monster4");

    Player.attackmiss = "";
    Battle.math = Math.random()/5 + 0.5;
    RenderMap();
    if (Math.random() <= Monster4.evade/100)
    {
    Player.attackmiss = "Miss";
    }
    else
    {
    Player.attackdamage = Math.floor(Player.tempstr*Player.tempattack*Battle.math/Monster4.defence);
    Monster4.hp = Monster4.hp - Player.attackdamage;
    }
    ShowMonster4Damage(Player.attackdamage)
    BattleMenu_Itemused();
}

function BattleMenu_Run()
{
  if (Battle.runable == false)
  {
  Talk_Window("Can't Run!", "blank.png");
  Delay(200);
  GoToActivePlayer();
  }
  else if (Player.active == "Player1")
  {
    Battle.runchance = Math.floor(500*Math.random() + Player.luck)      
    if (Battle.runchance >= 300)
    {
      BattleEnd();
    }
    else
    {
    Talk_Window("Failed", "blank.png")
    BattleMenuPlayer2();

    }
  }
  else if (Player.active == "Player2")
  {
    Battle.runchance = Math.floor(500*Math.random() + Player2.luck)      
    if (Battle.runchance >= 300)
    {
      BattleEnd();
    }
    else
    {
    Talk_Window("Failed", "blank.png")
    BattleMenuPlayer3();

    }
  }
  else if (Player.active == "Player3")
  {
    Battle.runchance = Math.floor(500*Math.random() + Player3.luck)      
    if (Battle.runchance >= 300)
    {
      BattleEnd();
    }
    else
    {
    Talk_Window("Failed", "blank.png")
    MonstersTurn();
    }
  }
}

//------------------Monsters Turn------------------

function MonstersTurn()
{
  if (Player.hp > 0)
  {
    if (Player.modreg == true)
    {
    Player.hp = Player.hp + Math.floor(Player.maxhp/10);
      if (Player.hp > Player.maxhp)
      {
      Player.hp = Player.maxhp;
      }
    RenderMap();
    HpFont.drawTextBox (Player.P1x, Player.P1y, 500, 100, 5, Math.floor(Player.maxhp/10));
    DrawPlayerStats();
    BattleName();
    FlipScreen();
    Delay(1000);
    }
  }
  if (Player2.hp > 0)
  {
    if (Player2.modreg == true)
    {
    Player2.hp = Player2.hp + Math.floor(Player2.maxhp/10);
      if (Player2.hp > Player2.maxhp)
      {
      Player2.hp = Player2.maxhp;
      }
    RenderMap();
    HpFont.drawTextBox (Player.P2x, Player.P2y, 500, 100, 5, Math.floor(Player2.maxhp/10));
    DrawPlayerStats();
    BattleName();
    FlipScreen();
    Delay(1000);
    }
  }
  if (Player3.hp > 0)
  {
    if (Player3.modreg == true)
    {
    Player3.hp = Player3.hp + Math.floor(Player3.maxhp/10);
      if (Player3.hp > Player3.maxhp)
      {
      Player3.hp = Player3.maxhp;
      }
    RenderMap();
    HpFont.drawTextBox (Player.P3x, Player.P3y, 500, 100, 5, Math.floor(Player3.maxhp/10));
    DrawPlayerStats();
    BattleName();
    FlipScreen();
    Delay(1000);
    }
  }
  if (Player.hp > 0)
  {
    if (Player.modmpreg == true)
    {
    Player.mp = Player.mp + 15;
      if (Player.mp > Player.maxmp)
      {
      Player.mp = Player.maxmp;
      }
    RenderMap();
    MpFont.drawTextBox (Player.P1x, Player.P1y, 500, 100, 5, 15);
    DrawPlayerStats();
    BattleName();
    FlipScreen();
    Delay(1000);
    }
  }
  if (Player2.hp > 0)
  {
    if (Player2.modmpreg == true)
    {
    Player2.mp = Player2.mp + 15;
      if (Player2.mp > Player2.maxmp)
      {
      Player2.mp = Player2.maxmp;
      }
    RenderMap();
    MpFont.drawTextBox (Player.P2x, Player.P2y, 500, 100, 5, 15);
    DrawPlayerStats();
    BattleName();
    FlipScreen();
    Delay(1000);
    }
  }
  if (Player3.hp > 0)
  {
    if (Player3.modmpreg == true)
    {
    Player3.mp = Player3.mp + 15;
      if (Player3.mp > Player3.maxmp)
      {
      Player3.mp = Player3.maxmp;
      }
    RenderMap();
    HpFont.drawTextBox (Player.P3x, Player.P3y, 500, 100, 5, 15);
    DrawPlayerStats();
    BattleName();
    FlipScreen();
    Delay(1000);
    }
  }

  if (Battle.monsternum <= 0)
  {
  BattleEnd();
  }
  else
  {
  Monster1Turn();
  }
}

//this is to choose between magic, run and fight for monsters
function Monster1Turn()
{
UpdatePlayers();
RenderMap();
Delay(200);
  if (Monster1.hp > 0)
  {
    if (Monster1.stun > 0)
    {
    Monster1.stun = Monster1.stun - 1;
    Monster2Turn();
    }
    else if (Monster1.run >= Monster1.hp)
    {
      if (Math.random() < Monster1.runchance)
      {
      Monster1.exp = 0;
      Monster1.gold = 0;
      Monster1.hp = 0;
      Monster1.mp = 0;
      
      WindowStyle.drawWindow(5, 5, 235, 12);
      Font.drawText(7, 7, Monster1.name + " Runs!");    
      DrawPlayerStats();
      BattleName();
      FlipScreen();
      Delay(500);
      DestroyPerson("Monster1");
      Battle.monsternum = Battle.monsternum - 1;
      Monster2Turn();
      }
      else
      {
      Player.active = "Monster1";
      AI("Monster1", Monster1.ai)
      }
    }
    else
    {
    Player.active = "Monster1";
    AI("Monster1", Monster1.ai)
    }  
  }
  else
  {
  Monster2Turn();
  }
}
function Monster2Turn()
{
UpdatePlayers();
RenderMap();
Delay(200);
  if (Monster2.hp > 0)
  {
    if (Monster2.stun > 0)
    {
    Monster2.stun = Monster2.stun - 1;
    Monster3Turn();
    }
    else if (Monster2.run >= Monster2.hp)
    {
      if (Math.random() < Monster2.runchance)
      {
      Monster2.exp = 0;
      Monster2.gold = 0;
      Monster2.hp = 0;
      Monster2.mp = 0;
    
      WindowStyle.drawWindow(5, 5, 235, 12);
      Font.drawText(7, 7, Monster2.name + " Runs!");    
      DrawPlayerStats();
      BattleName();
      FlipScreen();
      Delay(500);
      DestroyPerson("Monster2");
      Battle.monsternum = Battle.monsternum - 1;
      Monster3Turn();
      }
      else
      {
      Player.active = "Monster2";
      AI("Monster2", Monster2.ai)
      }
    }
    else
    {
    Player.active = "Monster2";
    AI("Monster2", Monster2.ai)
    }
  }
  else
  {
  Monster3Turn();
  }
}
function Monster3Turn()
{
UpdatePlayers();
RenderMap();
Delay(200);
  if (Monster3.hp > 0)
  {
    if (Monster3.stun > 0)
    {
    Monster3.stun = Monster3.stun - 1;
    Monster4Turn();
    }
    else if (Monster3.run >= Monster3.hp)
    {
      if (Math.random() < Monster3.runchance)
      {
      Monster3.exp = 0;
      Monster3.gold = 0;
      Monster3.hp = 0;
      Monster3.mp = 0;
    
      WindowStyle.drawWindow(5, 5, 235, 12);
      Font.drawText(7, 7, Monster3.name + " Runs!");    
      DrawPlayerStats();
      BattleName();
      FlipScreen();
      Delay(500);
      DestroyPerson("Monster3");
      Battle.monsternum = Battle.monsternum - 1;
      Monster4Turn();
      }
      else
      {
      Player.active = "Monster3";
      AI("Monster3", Monster3.ai)
      }
    }
    else
    {
    Player.active = "Monster3";
    AI("Monster3", Monster3.ai)
    }
  }
  else
  {
  Monster4Turn();
  }
}
function Monster4Turn()
{
UpdatePlayers();
RenderMap();
Delay(200);
  if (Monster4.hp > 0)
  {
    if (Monster4.stun > 0)
    {
    Monster4.stun = Monster4.stun - 1;
    BattleMenuPlayer1();
    }
    else if (Monster4.run >= Monster4.hp)
    {
      if (Math.random() < Monster1.runchance)
      {
      Monster4.exp = 0;
      Monster4.gold = 0;
      Monster4.hp = 0;
      Monster4.mp = 0;
    
      WindowStyle.drawWindow(5, 5, 235, 12);
      Font.drawText(7, 7, Monster4.name + " Runs!");    
      DrawPlayerStats();
      BattleName();
      FlipScreen();
      Delay(500);
      DestroyPerson("Monster4");
      Battle.monsternum = Battle.monsternum - 1;
      BattleMenuPlayer1();
      }
      else
      {
      Player.active = "Monster4";
      AI("Monster4", Monster4.ai)
      }
    }
    else
    {
    Player.active = "Monster4";
    AI("Monster4", Monster4.ai)
    }
  }
  else
  {
  BattleMenuPlayer1();
  }
}

function Monster1Attack()
{
SelectPTarget();
RenderMap();
Delay(200);
LoadDefStats();
BattleName();
DrawPlayerStats();

Player.mathtemp = Math.random()
Player.active = "Monster1";
Battle.math = Math.random()/5 + 0.5;
ShowPersonFight();
InjureFrame(Player.PTarget);
RenderMap();
if (Monster1.hp > 0)
{
  if (Player.attackmiss == "Miss")
  {
    TakeOutPlayerHealth();
    if (Player.dead == 3)
    {
    GarbageCollect();
    GameOver();
    }
    else
    {
    Monster2Turn();
    }
  }
  else
  {
    Player.attackdamage = Math.floor(Monster1.attack*Monster1.attack*Battle.math/Player.tempdef);
    TakeOutPlayerHealth();

    if (Player.dead == 3)
    {
    GarbageCollect();
    GameOver();
    }
    else
    {
    Monster2Turn();
    }
  }
}
else
{
Monster2Turn();
}
}

function Monster2Attack()
{
SelectPTarget();
RenderMap();
Delay(200);
LoadDefStats();
BattleName();
DrawPlayerStats();

Player.mathtemp = Math.random()
Player.active = "Monster2";
Battle.math = Math.random()/5 + 0.5;
ShowPersonFight();
InjureFrame(Player.PTarget);
RenderMap();

if (Monster2.hp > 0)
{
  if (Player.attackmiss == "Miss")
  {
    TakeOutPlayerHealth();
    if (Player.dead == 3)
    {
    GarbageCollect();
    GameOver();
    }
    else
    {
    Monster3Turn();
    }
  }
  else
  {
    Player.attackdamage = Math.floor(Monster2.attack*Monster2.attack*Battle.math/Player.tempdef);
    TakeOutPlayerHealth();
    if (Player.dead == 3)
    {
    GarbageCollect();
    GameOver();
    }
    else
    {
    Monster3Turn();
    }
  }
}
else
{
Monster3Turn();
}
}

function Monster3Attack()
{
SelectPTarget();
RenderMap();
Delay(200);
LoadDefStats();
BattleName();
DrawPlayerStats();

Player.active = "Monster3";
Battle.math = Math.random()/5 + 0.5;

ShowPersonFight();
InjureFrame(Player.PTarget)
RenderMap();

if (Monster3.hp > 0)
{
  if (Player.attackmiss == "Miss")
  {
    TakeOutPlayerHealth();
    if (Player.dead == 3)
    {
    GarbageCollect();
    GameOver();
    }
    else
    {
    Monster4Turn();
    }

  }
  else
  {
    Player.attackdamage = Math.floor(Monster3.attack*Monster3.attack*Battle.math/Player.tempdef);
    TakeOutPlayerHealth();
    if (Player.dead == 3)
    {
    GarbageCollect();
    GameOver();
    }
    else
    {
    Monster4Turn();
    }
  }
}
else
{
Monster4Turn();
}
}

function Monster4Attack()
{
SelectPTarget();
RenderMap();
Delay(200);
LoadDefStats();

Player.mathtemp = Math.random()
Player.active = "Monster4";
Battle.math = Math.random()/5 + 0.5;
ShowPersonFight();
InjureFrame(Player.PTarget)
RenderMap();

if (Monster4.hp > 0)
{
  if (Player.attackmiss == "Miss")
  {
    TakeOutPlayerHealth();
    if (Player.dead == 3)
    {
    GarbageCollect();
    GameOver();
    }
    else
    {
    BattleMenuPlayer1();
    }
  }
  else
  {
    Player.attackdamage = Math.floor(Monster4.attack*Monster4.attack*Battle.math/Player.tempdef);
    TakeOutPlayerHealth();
    if (Player.dead == 3)
    {
    GarbageCollect();
    GameOver();
    }
    else
    {
    BattleMenuPlayer1();
    }
  }
}
else
{
BattleMenuPlayer1();
}
}

function LoadFightStats()
{
  if (Player.active == "Player1")
  {
  Player.tempstr = Player.str;
  Player.atempstr = Player.tempystr
  Player.tempattack = Player.attack;
  Player.tempevade = Player.evade;
  }
  else if (Player.active == "Player2")
  {
  Player.tempstr = Player2.str;
  Player.atempstr = Player2.tempystr
  Player.tempattack = Player2.attack;
  Player.tempevade = Player2.evade;
  }
  else if (Player.active == "Player3")
  {
  Player.tempstr = Player3.str;
  Player.atempstr = Player3.tempystr;
  Player.tempattack = Player3.attack;
  Player.tempevade = Player3.evade;
  }
}
function LoadDefStats()
{
  if (Player.PTarget == "Player1")
  {
  Player.tempdef = Player.defence
  Player.tempmag = Player.mattack
  Player.templuck = Player.luck
  Player.temphp = Player.hp
  }
  else if (Player.PTarget == "Player2")
  {
  Player.tempdef = Player2.defence
  Player.tempmag = Player2.mattack
  Player.templuck = Player2.luck
  Player.temphp = Player2.hp
  }
  else if (Player.PTarget == "Player3")
  {
  Player.tempdef = Player3.defence
  Player.tempmag = Player3.mattack
  Player.templuck = Player3.luck
  Player.temphp = Player3.hp
  }
}

function ShowMonster1Damage(damage)
{
  RenderMap();
  if (Player.attackmiss == "Miss")
  {
  Font.drawTextBox (Monster1.posx, Monster1.posy, 500, 100, 5, "Miss");   
  }
  else if (damage < 0)
  {
  HpFont.drawTextBox (Monster1.posx, Monster1.posy, 500, 100, 5, damage*(0 - 1));
  }
  else
  { 
  Font.drawTextBox (Monster1.posx, Monster1.posy, 500, 100, 5, damage);
  }
  Player.attackmiss = "";
  DrawPlayerStats();
  BattleName();
  FlipScreen();
      
  Delay(1000);
  IsMonster1Dead();  
}
function ShowMonster2Damage(damage)
{
  RenderMap();
  if (Player.attackmiss == "Miss")
  {
  Font.drawTextBox (Monster2.posx, Monster2.posy, 500, 100, 5, "Miss");   
  }
  else if (damage < 0)
  {
  HpFont.drawTextBox (Monster2.posx, Monster2.posy, 500, 100, 5, damage*(0 - 1));
  }
  else
  { 
  Font.drawTextBox (Monster2.posx, Monster2.posy, 500, 100, 5, damage);
  }
  Player.attackmiss = "";
  DrawPlayerStats();
  BattleName();
  FlipScreen();
      
  Delay(1000);
  IsMonster2Dead();  
}
function ShowMonster3Damage(damage)
{
  RenderMap();
  if (Player.attackmiss == "Miss")
  {
  Font.drawTextBox (Monster3.posx, Monster3.posy, 500, 100, 5, "Miss");   
  }
  else if (damage < 0)
  {
  HpFont.drawTextBox (Monster3.posx, Monster3.posy, 500, 100, 5, damage*(0 - 1));
  }
  else
  { 
  Font.drawTextBox (Monster3.posx, Monster3.posy, 500, 100, 5, damage);
  }
  Player.attackmiss = "";

  DrawPlayerStats();
  BattleName();
  FlipScreen();
      
  Delay(1000);
  IsMonster3Dead();  
}
function ShowMonster4Damage(damage)
{
  RenderMap();
  if (Player.attackmiss == "Miss")
  {
  Font.drawTextBox (Monster4.posx, Monster4.posy, 500, 100, 5, "Miss");   
  }
  else if (damage < 0)
  {
  HpFont.drawTextBox (Monster4.posx, Monster4.posy, 500, 100, 5, damage*(0 - 1));
  }
  else
  { 
  Font.drawTextBox (Monster4.posx, Monster4.posy, 500, 100, 5, damage);
  }
  DrawPlayerStats();
  BattleName();
  FlipScreen();
  Player.attackmiss = "";
      
  Delay(1000);
  IsMonster4Dead();  
}

function SelectPTarget()
{
Player.attackmiss = "";

  if (Math.random() <= 0.33)
  {
    if (Player.hp > 0)
    {
    Player.PTarget = "Player1"
      if (Math.random() <= Player.evade/100)
      {
      Player.attackmiss = "Miss"
      }
      else
      {
      Player.attackmiss = "";
      }
    }
    else
    {
    SelectPTarget();
    }
  }  
  else if (Math.random() <= 0.5)
  {
    if (Player2.pres == true)
    {
      if (Player2.hp > 0)
      {
      Player.PTarget = "Player2"
        if (Math.random() <= Player2.evade/100)
        {
        Player.attackmiss = "Miss";
        }
        else
        {
        Player.attackmiss = "";
        }
      }
      else
      {
      SelectPTarget();
      }
    }  
    else 
    {
    SelectPTarget();
    }
  }
  else
  {
    if (Player3.pres == true)
    {
      if (Player3.hp > 0)
      {
      Player.PTarget = "Player3"
        if (Math.random() <= Player3.evade/100)
        {
        Player.attackmiss = "Miss";
        }
        else
        {
        Player.attackmiss = "";
        }
      }
      else
      {
      SelectPTarget();
      }
    }  
    else 
    {
    SelectPTarget();
    }
  }
}  

function TakeOutPlayerHealth()
{
  if (Player.PTarget == "Player1")
  {
    if (Player.attackmiss == "Miss")
    {
    Font.drawTextBox (Player.P1x, Player.P1y, 500, 100, 5, "Miss");
    UpdatePlayers();
    }
    else
    {  
    Player.hp = Player.hp - Player.attackdamage;
      if (Player.hp <= 0)
      {
      Player.hp = 0;
      }
    UpdatePlayers();      
    Font.drawTextBox (Player.P1x, Player.P1y, 500, 100, 5, Player.attackdamage);
    }
    
  DrawPlayerStats();
  BattleName();
  FlipScreen();
  Delay(1000);
  IsPlayerDead();
  }
  else if (Player.PTarget == "Player2")
  {
    if (Player.attackmiss == "Miss")
    {
    Font.drawTextBox (Player.P2x, Player.P2y, 500, 100, 5, "Miss");
    UpdatePlayers();
    }
    else
    {  
    Player2.hp = Player2.hp - Player.attackdamage;
      if (Player2.hp <= 0)
      {
      Player2.hp = 0;
      }
    UpdatePlayers();
    Font.drawTextBox (Player.P2x, Player.P2y, 500, 100, 5, Player.attackdamage);
    }
    
    DrawPlayerStats();
    BattleName();
    FlipScreen();
    Delay(1000);
    IsPlayerDead();
  }
  else if (Player.PTarget == "Player3")
  {
    if (Player.attackmiss == "Miss")
    {
    Font.drawTextBox (Player.P3x, Player.P3y, 500, 100, 5, "Miss");
    UpdatePlayers();
    }
    else
    {  
    Player3.hp = Player3.hp - Player.attackdamage;
      if (Player3.hp <= 0)
      {
      Player3.hp = 0;
      }
    UpdatePlayers();
    Font.drawTextBox (Player.P3x, Player.P3y, 500, 100, 5, Player.attackdamage);
    }
    
  DrawPlayerStats();
  BattleName();
  FlipScreen();
  Delay(1000);
  IsPlayerDead();
  }  
}

function LoadMonsterStats()
{
  if (Player.active == "Monster1")
  {
  Battle.Mx = Monster1.posx;
  Battle.My = Monster1.posy;
  Battle.Mattack = Monster1.attack;
  Battle.Mmag = Monster1.mag;
  Battle.Mluck = Monster1.luck;
  Battle.Mname = "Monster1";
  Battle.Mmp = Monster1.mp;
  Battle.Mhp = Monster1.hp;
  Battle.Mmaxhp = Monster1.maxhp;
  Battle.Mspecial1 = Monster1.special1;
  Battle.Mspecial2 = Monster1.special2;
  Battle.Mspecial3 = Monster1.special3;
  Battle.Mspecial4 = Monster1.special4;
  }
  else if (Player.active == "Monster2")
  {
  Battle.Mx = Monster2.posx;
  Battle.My = Monster2.posy;
  Battle.Mattack = Monster2.attack;
  Battle.Mmag = Monster2.mag;
  Battle.Mluck = Monster2.luck;
  Battle.Mname = "Monster2";
  Battle.Mmp = Monster2.mp;
  Battle.Mhp = Monster2.hp;
  Battle.Mmaxhp = Monster2.maxhp;
  Battle.Mspecial1 = Monster2.special4;
  Battle.Mspecial2 = Monster2.special4;
  Battle.Mspecial3 = Monster2.special4;
  Battle.Mspecial4 = Monster2.special4;
  }
  else if (Player.active == "Monster3")
  {
  Battle.Mx = Monster3.posx;
  Battle.My = Monster3.posy;
  Battle.Mattack = Monster3.attack;
  Battle.Mmag = Monster3.mag;
  Battle.Mluck = Monster3.luck;
  Battle.Mname = "Monster3";
  Battle.Mmp = Monster3.mp;
  Battle.Mhp = Monster3.hp;
  Battle.Mmaxhp = Monster3.maxhp;
  Battle.Mspecial1 = Monster3.special4;
  Battle.Mspecial2 = Monster3.special4;
  Battle.Mspecial3 = Monster3.special4;
  Battle.Mspecial4 = Monster3.special4;
  }
  else if (Player.active == "Monster4")
  {
  Battle.Mx = Monster4.posx;
  Battle.My = Monster4.posy;
  Battle.Mattack = Monster4.attack;
  Battle.Mmag = Monster4.mag;
  Battle.Mluck = Monster4.luck;
  Battle.Mname = "Monster4";
  Battle.Mmp = Monster4.mp;
  Battle.Mhp = Monster4.hp;
  Battle.Mmaxhp = Monster4.maxhp;
  Battle.Mspecial1 = Monster4.special4;
  Battle.Mspecial2 = Monster4.special4;
  Battle.Mspecial3 = Monster4.special4;
  Battle.Mspecial4 = Monster4.special4;
  }
}

function MonsterSpecial()
{
LoadMonsterStats();

  if (Math.random() <= 0.25)
  {
  MonsterSpecial1();
  }
  else if (Math.random() <= 0.33)
  {
  MonsterSpecial2();
  }
  else if (Math.random() <= 0.5)
  {
  MonsterSpecial3();
  }
  else
  {
  MonsterSpecial4();
  }
}

function ActiveMonsterAttack()
{
  if (Player.active == "Monster1")
  {
  Monster1Attack();
  }
  else if (Player.active == "Monster2")
  {
  Monster2Attack();
  }
  else if (Player.active == "Monster3")
  {
  Monster3Attack();
  }
  else
  {
  Monster4Attack();
  }
}
function GoToNextMonster()
{
  if (Player.active == "Monster1")
  {
  Monster2Turn();
  }
  else if (Player.active == "Monster2")
  {
  Monster3Turn();
  }
  else if (Player.active == "Monster3")
  {
  Monster4Turn();
  }
  else
  {
  BattleMenuPlayer1();
  }
}


function MonsterSpecial1()
{
Battle.MSpecial = Battle.Mspecial1;
MonsterSpecialSelect();
}
function MonsterSpecial2()
{
Battle.MSpecial = Battle.Mspecial2;
MonsterSpecialSelect();
}
function MonsterSpecial3()
{
Battle.MSpecial = Battle.Mspecial3;
MonsterSpecialSelect();
}
function MonsterSpecial4()
{
Battle.MSpecial = Battle.Mspecial4;
MonsterSpecialSelect();
}

